/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package do_.com.pbd.business.referrals.creditcardreferralprogramstatusupdater.impl;

import do_.com.bpd.hrtoolspersistence.configuration.ConfigManager;
import do_.com.pbd.business.referrals.creditcardreferralprogramstatusupdater.Installer;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Developer
 */
public class UpdaterController {

    public int secondsToMilliseconds(int seconds) {
        return seconds * 1000;
    }

    public int createUpdater() {
        try {
            NewFilesMonitor fm = new NewFilesMonitor();
        } catch (Exception ex) {
            return 0;
        }
        return 1;

    }

    public int removeUpdater() {
        try {
            System.setProperty(NewFilesMonitor.FILESMONITORKEEPMONITORING, "STOP");
            Installer.RUN_STATUS = "0";
            Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Poller stopped!");
            System.setProperty(NewFilesMonitor.NEWFILESMONITORBUSY, "0");
        } catch (Exception ex) {
            return 0;
        }
        return 1;

    }

    public int stopUpdater() {
        Installer.RUN_STATUS = "0";
        Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Poller stopped!");
        System.setProperty(NewFilesMonitor.NEWFILESMONITORBUSY, "0");
        return 0;
    }

    public int startUpdater() {
        Installer.RUN_STATUS = "1";
        Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Poller started or resumed!");
        System.setProperty(NewFilesMonitor.NEWFILESMONITORBUSY, "0");
        return 1;
    }

    public boolean isRunning() {
        return (Installer.RUN_STATUS.equals("1")) ? true : false;
    }

    public void setMinimumPollingRate(int seconds) throws Exception {
        ConfigManager cm = new ConfigManager();
        cm.setConfig(Installer.MINIMUM_POLLING_RATE_CONFIG_NAME, String.valueOf(secondsToMilliseconds(seconds)));
        Installer.MINIMUM_POLLING_RATE = Integer.parseInt(cm.getConfig(Installer.MINIMUM_POLLING_RATE_CONFIG_NAME));
        if (Installer.CURRENT_POLLING_RATE < Installer.MINIMUM_POLLING_RATE) {
            Installer.CURRENT_POLLING_RATE = Installer.MINIMUM_POLLING_RATE;
            cm.setConfig(Installer.CURRENT_POLLING_RATE_CONFIG_NAME, String.valueOf(Installer.MINIMUM_POLLING_RATE));
        }
        Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Minimum polling rate changed to {0}", secondsToMilliseconds(seconds));
    }

    public void setCurrentPollingRate(int seconds) throws Exception {
        if (secondsToMilliseconds(seconds) < Installer.MINIMUM_POLLING_RATE) {
            throw new Exception("Polling cannot be less than " + (Installer.MINIMUM_POLLING_RATE / (1000 * 1)) + ". Try setting a higher polling rate.");
        }
        ConfigManager cm = new ConfigManager();
        cm.setConfig(Installer.CURRENT_POLLING_RATE_CONFIG_NAME, String.valueOf(secondsToMilliseconds(seconds)));
        Installer.CURRENT_POLLING_RATE = Integer.parseInt(cm.getConfig(Installer.CURRENT_POLLING_RATE_CONFIG_NAME));
        Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Current polling rate changed to {0}", secondsToMilliseconds(seconds));
    }

//    public void setPollingInterval(int seconds) throws Exception {
//        if (secondsToMilliseconds(seconds) < Installer.MINIMUM_POLLING_RATE) {
//            throw new Exception("Polling cannot be less than " + Installer.MINIMUM_POLLING_RATE + ". Try setting a higher polling rate.");
//        }
//        ConfigManager cm = new ConfigManager();
//        cm.setConfig(Installer.MINIMUM_POLLING_RATE_CONFIG_NAME, String.valueOf(secondsToMilliseconds(seconds)));
//        Installer.MINIMUM_POLLING_RATE = Integer.parseInt(cm.getConfig(Installer.MINIMUM_POLLING_RATE_CONFIG_NAME));
//        Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Minimum polling rate changed to {0}", secondsToMilliseconds(seconds));
//    }
    public void setRepository(String path) throws Exception {
        File f = new File(path);
        boolean isValid = true;
        if (!f.exists()) {
            isValid = false;

        }
        if (!f.canWrite() || !f.canRead()) {
            isValid = false;
        }
        if (!f.isDirectory()) {
            isValid = false;
        }

//        if (f.isHidden()) {
//            isValid = false;
//        }

        if (!isValid) {
            throw new Exception("Invalid directory. Make sure that it exists, is read/write-able, is not hidden and is directory");
        } else {
            ConfigManager cm = new ConfigManager();
            cm.setConfig(Installer.REPOSITORY_CONFIG_NAME, path);
            Installer.CASES_REPOSITORY = cm.getConfig(Installer.REPOSITORY_CONFIG_NAME);
            Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Cases repository changed to " + path);
            System.setProperty(NewFilesMonitor.NEWFILESMONITORBUSY, "0");
        }
    }
}
